/*
 * Copyright (C) 2020 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.xuexiang.xui_lib.component.button.shinebutton;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.element.Element;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.image.PixelMap;
import ohos.media.image.common.Size;

/**
 * PorterImageView
 *
 * @author xuexiang
 * @since 2020-01-06
 */
public abstract class PorterImageView extends Image {
    static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "MY_TAG");
//    Element element;
    private DrawTask mDrawTask = (component, canvas) -> this.onDraw(component, canvas);
//    private Canvas mMaskCanvas;
//    private PixelMap mMaskBitmap;
    private Paint mMaskPaint;

//    private Canvas mDrawableCanvas;
//    private PixelMap mDrawableBitmap;
    private Paint mDrawablePaint;
//    private Size estimateSize;
    private boolean hasSetSizeBeforeDraw = false;

    //private int mPaintColor = Color.GRAY.getValue();

    //private boolean mInvalidated = true;

    /**
     * PorterImageView
     *
     * @param context
     */
    public PorterImageView(Context context) {
        super(context);
        init();
    }

    /**
     * PorterImageView
     *
     * @param context
     * @param attrs
     */
    public PorterImageView(Context context, AttrSet attrs) {
        super(context, attrs);
        init();
    }

    /**
     * PorterImageView
     *
     * @param context
     * @param attrs
     * @param defStyle
     */
    public PorterImageView(Context context, AttrSet attrs, int defStyle) {
        super(context, attrs, "");
        init();
    }

    private void init() {
        mMaskPaint = new Paint();
        mMaskPaint.setColor(Color.BLACK);

        addDrawTask(mDrawTask);
    }

    private Size doEstimateSize(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSize = EstimateSpec.getSize(widthMeasureSpec);
        int heightSize = EstimateSpec.getSize(heightMeasureSpec);
        int width = Math.min(widthSize, heightSize);
        return new Size(width, width);
    }

    /**
     * setTintColor
     *
     * @param color
     */
    public void setTintColor(int color) {
        //mPaintColor = color;
        if (mDrawablePaint != null) {
            mDrawablePaint.setColor(new Color(color));
            invalidate();
        }
    }

    @Override
    public void invalidate() {
        //mInvalidated = true;
        super.invalidate();
    }

    /**
     * paintMaskCanvas
     *
     * @param maskCanvass
     * @param maskPaint
     * @param width
     * @param height
     * @return Element
     */
    protected abstract Element paintMaskCanvas(Canvas maskCanvass, Paint maskPaint, int width, int height);

    /**
     * onDraw
     *
     * @param component
     * @param canvas
     */
    public void onDraw(Component component, Canvas canvas) {
        if (!hasSetSizeBeforeDraw) {
            hasSetSizeBeforeDraw = true;
        }
    }
}